﻿using CS.model;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace CS.DAL
{
    public class AdminSelect
    {
        private DBHelper com = DBHelper.GetDBHelper();
        public List<Admininfo> SelectAdmin(string Name)
        {
            List<Admininfo> list = new List<Admininfo>();
            string sql = "select * from Userinfo where UsersName like '%" + Name + "%'";
            DbDataReader dr = com.GetReader(sql);
            while (dr.Read())
            {
                Admininfo s = new Admininfo();
                s.UserName = dr["UsersName"].ToString();
                s.UserPass = dr["UsersPwd"].ToString();
                list.Add(s);
            }
            com.Close();
            dr.Close();
            return list;
        }
        public bool InsertAdmin(string adminname, string adminpwd)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=UserInfo;Integrated Security=True");
            try
            {
                //TODO:后续使用事务完善程序流程
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                //1.插入订单
                string sql = string.Format("insert into Userinfo (UsersName,UsersPwd) values ({0},{1})", adminname, adminpwd);
                cmd.CommandText = sql;
                int count = cmd.ExecuteNonQuery();//执行插入语句获取受影响的行数
                if (count == 0)
                {//判断是否插入数据成功
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
            return true;
        }
    }
}
